﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Sol_NetCoreDemo.JWT
{
    public class TokenJWT
    {
        public static TokenResponseModel GenerateToken(string userName)
        {
            TokenResponseModel response = new TokenResponseModel();

            var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("!my secret key is ULT72!ULT72!ULT72!ULT72!ULT72!"));

            var claims = new Claim[]
            {
                new Claim("Account",userName)

            };

            var token = new JwtSecurityToken(
                issuer: "TokenServer",
                audience: "audience",
                claims: claims,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddDays(7),
                signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256)
            );

            response.access_token = new JwtSecurityTokenHandler().WriteToken(token);

            response.type = "bearer";

            response.expires = DateTime.Now.AddDays(7);

            return response;
        }
    }

    public class TokenResponseModel
    {
        public string type { get; set; }

        public string access_token { get; set; }

        public DateTime expires { get; set; }
    }
}
